package cn.tedu.mall.seckill.timer.job;

import cn.tedu.mall.seckill.mapper.SeckillSpuMapper;
import cn.tedu.mall.seckill.utils.RedisBloomUtils;
import cn.tedu.mall.seckill.utils.SeckillCacheUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.Arrays;

@Slf4j
public class SeckillBloomJob implements Job {
    @Autowired
    private RedisBloomUtils redisBloomUtils;
    @Autowired
    private SeckillSpuMapper seckillSpuMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String bloomFilterKey = SeckillCacheUtils.getBloomFilterKey(LocalDate.now());
        Long[] allSeckillSpuIds = seckillSpuMapper.findAllSeckillSpuIds();
        String[] spuIdsStr = new String[allSeckillSpuIds.length];
        for(int i=0; i< spuIdsStr.length; i++){
            spuIdsStr[i] = allSeckillSpuIds[i].toString();
        }
        redisBloomUtils.bfmadd(bloomFilterKey, spuIdsStr);
        log.info("布隆过滤器加载完成。");
    }
}
